local tianli = fk.CreateSkill{
  name = "kq__tianli",
  tags = { Skill.Lord },
  attached_skill_name = "kq__tianli_active&",
}

Fk:loadTranslationTable{
  ["kq__tianli"] = "天立",
  [":kq__tianli"] = "主公技，一名群势力角色的出牌阶段开始时，其可以重铸或交给你一张【杀】。",
  ["$kq__tianli1"] = "得君相助，自当反其庸主。",
  ["$kq__tianli2"] = "汝识时务，势成天下则立",
  ["#ask__shooose"] = "交给 %src 一张【杀】"
}

tianli:addAcquireEffect(function (self, player)
  local room = player.room
  for _, p in ipairs(room:getOtherPlayers(player, false)) do
    if p.kingdom == "qun" then
      room:handleAddLoseSkills(p, "kq__tianli_active&", nil, false, true)
    else
      room:handleAddLoseSkills(p, "-kq__tianli_active&", nil, false, true)
    end
  end
end)

tianli:addEffect(fk.AfterPropertyChange, {
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player.kingdom == "qun" and table.find(room.alive_players, function (p)
      return p ~= player and p:hasSkill(tianli.name, true)
    end) then
      room:handleAddLoseSkills(player, tianli.attached_skill_name, nil, false, true)
    else
      room:handleAddLoseSkills(player, "-" .. tianli.attached_skill_name, nil, false, true)
    end
  end,
})

tianli:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target==player and player:hasSkill(tianli.name) and target.phase == target.Play and target.kingdom == "qun"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(target, {
        choices = {"重铸一张【杀】" , "#ask__shooose:"..player.id , "Cancel"},
        skill_name = tianli.name
      })
      if choice == "重铸一张【杀】" then
        local cards = room:askToCards(target, {
            min_num = 1,
            max_num = 1,
            skill_name = tianli.name,
            pattern = "slash",
            prompt = "请选择一张【杀】",
            cancelable = true,
        })
        room:recastCard(cards, target, tianli.name)
      elseif choice == "#ask__shooose" then
        local cards = room:askToCards(target, {
            min_num = 1,
            max_num = 1,
            skill_name = tianli.name,
            pattern = "slash",
            prompt = "请选择一张【杀】",
            cancelable = true,
        })
        room:obtainCard(player, cards, false, fk.ReasonGive)
      end
  end,
})


return tianli
